package property.controller;

import com.alibaba.fastjson.JSONObject;
import property.pojo.Overhaul;
import property.pojo.OverhaulKey;
import property.pojo.Staff;
import property.service.OverhaulService;
import property.service.impl.OverhaulServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

@WebServlet(urlPatterns = {"/Overhaul/*"})
public class OverhaulServlet extends BaseServlet {
    private OverhaulService overhaulService = new OverhaulServiceImpl();

    public void addOverhaul(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        if(session.getAttribute("currentUser").getClass() != Staff.class){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }
        Staff s = (Staff) session.getAttribute("currentUser");

        Overhaul overhaul = JSONObject.toJavaObject(jb.getJSONObject("insert"),Overhaul.class);
        overhaul.setoStaffId(s.getsId());
        int i = overhaulService.addOverhaul(overhaul);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "接取检修工作成功");
        }else if(i == -2) {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "已接过该检修工作或不存在该检修");
        } else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void deleteOverhaul(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        OverhaulKey overhaulKey = JSONObject.toJavaObject(jb, OverhaulKey.class);

        if(character == 0){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }else if(character == 1 && !Objects.equals(overhaulKey.getoStaffId(), ((Staff) session.getAttribute("currentUser")).getsId())){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }
        int i = overhaulService.deleteOverhaul(overhaulKey);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "删除检修记录成功");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

    public void updateOverhaul(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");

        if(character != 1){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }

        Overhaul overhaul = JSONObject.toJavaObject(jb.getJSONObject("update"),Overhaul.class);
        if(((Staff)session.getAttribute("currentUser")).getsId() != overhaul.getoStaffId()){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }
//        overhaul.setoInfraId(jb.getJSONObject("update").getInteger("oInfraId"));
//        overhaul.setoStaffId(jb.getJSONObject("update").getInteger("oStaffId"));
        int i = overhaulService.updateOverhaul(overhaul);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "删除检修记录成功");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }
}
